#include "stdafx.h"

void game_machine :: cast_cmd   (short emit, short abil, short target)
{
    // here we check for invalid commands, like mana, range and cooldown
    LOGMSG("[cmd] "+ inspect_cast(emit, abil, target));
	// check range, obstacle, mana, cooldown
	if(Abilities[abil].on_cooldown) // cooldown check
    { error_message(is_on_cooldown); return; }

    // if(Characters[emit].mana < AbilBases[Characters[emit].Abilities[abil].abil].cost)
    if(Characters[emit].mana < Abilities[abil].cost)
    { error_message(out_of_mana); return; }

    if(Abilities[abil].range == 0) // self emit, no range check or missile emit
    { cast_start(emit, abil, target); return; }

    float range_sqrd = float(Abilities[abil].range); // will be used in both target and aoe
    range_sqrd *= range_sqrd;
    //cout << range_sqrd << " " CharPositions[emit] << CharPositions[target]
    if(Abilities[abil].mask & MISSILE) // requires target ?
       if(sqrd_dist(CharPositions[emit], CharPositions[target]) > range_sqrd)
        { error_message(too_far_away1); return; }
    else // doesn't require target, check for clicked position range
        if(sqrd_dist(CharPositions[emit], click_position) > range_sqrd)
        { error_message(too_far_away2); return; }
    /* lacks obstacle check, will require raycast etc */
    cast_start(emit, abil, target);
}
